﻿using System;
using Autodesk.Revit.DB;
using YJKArchMethodLibrary.Utils;

namespace Aproll.DataDefine
{
	public class DoorInfo
	{
		public XYZ Position
		{
			get
			{
				return this.m_Position;
			}
		}

		public XYZ Orientation
		{
			get
			{
				return this.m_Orientation;
			}
		}

		public double Width
		{
			get
			{
				return this.m_Width;
			}
		}

		public double WallThickness
		{
			get
			{
				return this.m_WallThickness;
			}
		}

		public DoorInfo(DoorInfo rhs)
		{
			this.m_Position = rhs.m_Position;
			this.m_Orientation = rhs.m_Orientation;
			this.m_Width = rhs.m_Width;
			this.m_WallThickness = rhs.m_WallThickness;
		}

		public DoorInfo(XYZ position, XYZ orientation, double width, double wallThickness)
		{
			this.m_Position = position;
			this.m_Orientation = orientation;
			this.m_Width = width;
			this.m_WallThickness = wallThickness;
		}

		public void GetEndsOnEdgeInfo(EdgeInfo edgeInfo, ref XYZ ptStart, ref XYZ ptEnd)
		{
			if (!edgeInfo.IsArc)
			{
				XYZ xyz = Geometry.CalculateFootPoint(edgeInfo.StartPoint, edgeInfo.EndPoint, this.m_Position);
				XYZ normal = edgeInfo.Normal;
				ptStart = xyz - normal * this.m_Width / 2.0;
				ptEnd = xyz + normal * this.m_Width / 2.0;
				return;
			}
			XYZ center = edgeInfo.Center;
			XYZ pt = Geometry.CalculateFootPoint(edgeInfo.StartPoint, edgeInfo.EndPoint, center, this.m_Position);
			double num = Math.Asin(this.m_Width / (edgeInfo.Radius * 2.0));
			if (Geometry.IsEqual(edgeInfo.Normal, XYZ.BasisZ, 0.0001))
			{
				ptStart = Geometry.RotateTo(pt, center, XYZ.BasisZ, -num);
				ptEnd = Geometry.RotateTo(pt, center, XYZ.BasisZ, num);
				return;
			}
			ptStart = Geometry.RotateTo(pt, center, XYZ.BasisZ, num);
			ptEnd = Geometry.RotateTo(pt, center, XYZ.BasisZ, -num);
		}

		public bool IsLinked(EdgeInfo edgeInfo)
		{
			if (edgeInfo.IsArc)
			{
				XYZ center = edgeInfo.Center;
				XYZ xyz = Geometry.CalculateFootPoint(edgeInfo.StartPoint, edgeInfo.EndPoint, center, this.m_Position);
				if (!Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, center, edgeInfo.Normal, xyz, 1E-09))
				{
					return false;
				}
				double val = xyz.DistanceTo(this.m_Position);
				double radius = edgeInfo.Radius;
				double val2 = radius - Math.Sqrt(Math.Pow(radius, 2.0) - Math.Pow(this.Width / 2.0, 2.0));
				if (!Geometry.IsEqual(val2, val, this.m_WallThickness))
				{
					return false;
				}
				XYZ a = xyz - center;
				if (!Geometry.IsParallel(a, this.m_Orientation, 1E-09))
				{
					return false;
				}
			}
			else
			{
				XYZ xyz2 = Geometry.CalculateFootPoint(edgeInfo.StartPoint, edgeInfo.EndPoint, this.m_Position);
				if (!Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, xyz2, 1E-09))
				{
					return false;
				}
				double val3 = xyz2.DistanceTo(this.m_Position);
				if (Geometry.LessThan(this.m_WallThickness, val3))
				{
					return false;
				}
				if (!Geometry.IsVertical(edgeInfo.Normal, this.m_Orientation, 1E-09))
				{
					return false;
				}
			}
			return true;
		}

		private XYZ m_Position;

		private XYZ m_Orientation;

		private double m_Width;

		private double m_WallThickness;
	}
}
